/*
 * BackOfficeWindow.java
 *
 * Created on August 16, 2006, 12:43 PM
 */

package com.floreantpos.ui;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JPopupMenu;
import javax.swing.JTabbedPane;

import com.floreantpos.main.Application;
import com.floreantpos.swing.TransparentPanel;

/**
 *
 * @author  MShahriar
 */
public class ManagementWindow extends javax.swing.JFrame {

	TransparentPanel explorer;
	
	/** Creates new form BackOfficeWindow */
	public ManagementWindow() {
		initComponents();
		setTitle(Application.getTitle());
		
		tabbedPane.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					JPopupMenu menu = new JPopupMenu();
					menu.add(new AbstractAction(com.floreantpos.POSConstants.CLOSE) {

						public void actionPerformed(ActionEvent e) {
							int index = tabbedPane.getSelectedIndex();
							tabbedPane.remove(index);
						}

					});
					menu.show(tabbedPane, e.getX(), e.getY());
				}
			}
		});

		setSize(800, 600);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((screenSize.width - 800) >> 1, (screenSize.height - 600) >> 1);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				dispose();
			}
		});

	}
	
	private void initAction(TransparentPanel explorer){
		JTabbedPane tabbedPane = getTabbedPane();
		int index = tabbedPane.indexOfTab(explorer.getName());
		if (index == -1) {
			tabbedPane.addTab(explorer.getName(), explorer);
		}
		else {
			explorer = (TransparentPanel) tabbedPane.getComponentAt(index);
		}
		tabbedPane.setSelectedComponent(explorer);
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	private void initComponents() {
		jPanel1 = new javax.swing.JPanel();
		tabbedPane = new javax.swing.JTabbedPane();

		getContentPane().setLayout(new java.awt.BorderLayout(5, 0));

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		jPanel1.setLayout(new java.awt.BorderLayout(5, 0));

		jPanel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
		jPanel1.add(tabbedPane, java.awt.BorderLayout.CENTER);

		getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

		pack();
	}// </editor-fold>//GEN-END:initComponents

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new ManagementWindow().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JPanel jPanel1;
	private javax.swing.JTabbedPane tabbedPane;

	// End of variables declaration//GEN-END:variables

	public javax.swing.JTabbedPane getTabbedPane() {
		return tabbedPane;
	}

	public TransparentPanel getExplorer() {
		return explorer;
	}

	public void setExplorer(TransparentPanel explorer) {
		this.explorer = explorer;

		if(explorer != null){
			initAction(explorer);
			setTitle(Application.getTitle() + "- " + explorer.getName());
		}
		
	}

}
